package net.cloudbusniness.cloudbusinessclient.application;

import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import net.cloudbusniness.cloudbusinessclient.constant.Constant;
import net.cloudbusniness.cloudbusinessclient.mobieimsdk.ChatBaseEventImpl;
import net.cloudbusniness.cloudbusinessclient.mobieimsdk.ChatTransDataEventImpl;
import net.cloudbusniness.cloudbusinessclient.mobieimsdk.MessageQoSEventImpl;
import net.openmob.mobileimsdk.android.ClientCoreSDK;
import net.openmob.mobileimsdk.android.conf.ConfigEntity;

/**
 * Created by hulei on 2017/2/17.
 * app instance data
 */

public class AppInstance extends Application {
    public static Context appContext;
    public static String versioName;
    public static String TAG;

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = getApplicationContext();
        TAG = getPackageName();
        try {
            versioName = getVersionName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        initMobileImSdk();


    }

    /**
     * get appversion name
     */
    private String getVersionName() throws Exception {
        PackageManager packageManager = getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
        String versioName = packageInfo.versionName;
        return versioName;
    }

    /**
     * init MobileImSdk
     */
    private void initMobileImSdk() {
        // 设置AppKey
        ConfigEntity.appKey = Constant.appKey;

    // 设置服务器ip和服务器端口
        ConfigEntity.serverIP = Constant.serverIP;
        ConfigEntity.serverUDPPort =Constant.serverUDPPort;

    // 请确保首先进行核心库的初始化（这不同于iOS和Java端）
        ClientCoreSDK.getInstance().init(appContext);
        //设置事件回调监听
        ClientCoreSDK.getInstance().setChatBaseEvent(new ChatBaseEventImpl());
        ClientCoreSDK.getInstance().setChatTransDataEvent(new ChatTransDataEventImpl());
        ClientCoreSDK.getInstance().setMessageQoSEvent(new MessageQoSEventImpl());
    }
}
